from core import *

from doctype import Doctype
from tag import Tag

__all__=['Html', 'Head']

class Html(Tag):
    def __init__(self, author=None, title=None, description=None, keywords=None, doctype=Doctype('XHTML', 'Strict')):
        Tag.__init__(self, "html")
  
        self.addAttribute('xmlns', 'http://www.w3.org/1999/xhtml')
        self.addAttribute('xml:lang', 'en')
        self.addAttribute('lang', 'en')
        
        self.doctype = doctype
        self.head = self.addTag( Head(author, title, description, keywords) )
        self.body = self.addTag( 'body' )
        
    def toString(self):
        return self.doctype.toString()+'\n'+Tag.toString(self, 0, self.doctype)
    

class Head(Tag):
    def __init__(self, author=None, title=None, description=None, keywords=None):
        Tag.__init__(self, 'head')
        
        self.__title = self.addTag('title')
        self.__title.addAttribute('name', 'title')
        self.__title.addAttribute('content', title)
        self.__title.addContent(title)
        
        self.__author = self.addTag('meta')
        self.__author.addAttribute('name', 'author')
        self.__author.addAttribute('content', author)
        
        self.__description = self.addTag('meta')
        self.__description.addAttribute('name', 'description')
        self.__description.addAttribute('content', description)    
        
        self.__keywords = self.addTag('meta')
        self.__keywords.addAttribute('name', 'keywords')
        self.__keywords.addAttribute('content', keywords)
        
        self.__css = []
        self.__scripts = []
        
    def addStyleSheet(self, stylePath, inPos=-1):
        self.__css.append( self.addTag('link', inPos) )
        self.__css[-1].addAttribute('rel', 'stylesheet')
        self.__css[-1].addAttribute('type', 'text/css')
        self.__css[-1].addAttribute('href', stylePath)
        return self.__css[-1]
        
    def getStyleSheets(self):
        return self.__css
    
    def addScript(self, scriptPath, language='javascript', inPos=-1):
        self.__scripts.append( self.addTag('script', inPos) )
        self.__scripts[-1].addAttribute('type', 'text/%s' %language)
        self.__scripts[-1].addAttribute('src', scriptPath)
        self.__scripts[-1].addContent(None)
        return self.__scripts[-1]
        
    